import 'dart:io';
import 'dart:ui';

import 'package:common_utils/common_utils.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import 'package:nft_shop/login/controller/logic_controller.dart';
import 'package:nft_shop/login/controller/user_controller.dart';
import 'package:nft_shop/res/theme_base.dart';
import 'package:nft_shop/res/theme_light.dart';
import 'package:nft_shop/sift/conteroller/index_conteroller.dart';
import 'package:nft_shop/util/handle_error_utils.dart';
import 'package:nft_shop/util/image_utils.dart';
import 'package:nft_shop/util/log_utils.dart';
import 'package:nft_shop/widgets/web_page_transitions.dart';

class Global {
  static String AppName = "元界数藏";
  // static String AppHost = "https://nft.limaiwl.cn/";
  static String AppHost = "https://nft.limaiwluo.cn/";
  // static String AppHost = "http://www.xingkenft.com/";
  static late BaseTheme kTheme;
  static bool isDark = false;

  static String myDINFont = "DIN";
  static String myRegularFont = "PingFangSC-Regular";
  static String mySemiboldFont = "PingFangSC-Semibold";

  static final box = GetStorage();

  static bool get isAndroid => Platform.isAndroid;
  static bool get isIOS => Platform.isIOS;

  //初始化
  static Future init(VoidCallback callback) async {
    /// 确保初始化完成
    WidgetsFlutterBinding.ensureInitialized();
    ///Log初始化
    Log.init();
    LogUtil.init(isDebug: true, tag: '日志',maxLen: 800);
    EasyLoading.instance
      ..displayDuration = const Duration(milliseconds: 1000)
      ..indicatorType = EasyLoadingIndicatorType.fadingCircle
      ..loadingStyle = EasyLoadingStyle.dark
      ..userInteractions = false;
    await GetStorage.init();
    ///用户信息初始化
    UserController.to;
    LoginLogic.to;
    ///初始化
    Get.lazyPut(() => IndexConteroller());

    //配置黑白颜色
    kTheme = LightTheme();
    isDark = false;

    /// 异常处理
    handleError(callback());

    /// 隐藏状态栏。为启动页、引导页设置。完成后修改回显示状态栏。
    SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
        statusBarColor: Colors.transparent,
        statusBarBrightness: Brightness.light));
    // TODO(weilu): 启动体验不佳。状态栏、导航栏在冷启动开始的一瞬间为黑色，且无法通过隐藏、修改颜色等方式进行处理。。。
    // 相关问题跟踪：https://github.com/flutter/flutter/issues/73351
  }

  ThemeData getTheme() {
    return ThemeData(
      splashColor:Colors.transparent,
      highlightColor: Colors.transparent,
      brightness:  Brightness.light,
      primaryColor: kTheme.colorPrimary,
      // Tab指示器颜色
      indicatorColor: kTheme.textPrimary4,
      // 页面背景色
      scaffoldBackgroundColor: kTheme.colorBackground1,
      // 主要用于Material背景色
      canvasColor: kTheme.colorBackground1,
      // 文字选择色（输入框选择文字等）
      // textSelectionColor: Colours.app_main.withAlpha(70),
      // textSelectionHandleColor: Colours.app_main,
      // 稳定版：1.23 变更(https://flutter.dev/docs/release/breaking-changes/text-selection-theme)

      textTheme: TextTheme(
        // TextField输入文字颜色
        subtitle1: TextStyle(
            fontSize: 15,
            color: kTheme.textPrimary4,
            textBaseline: TextBaseline.alphabetic
        ),
        // Text文字样式
        bodyText2: TextStyle(
            fontSize: 15,
            color: kTheme.textPrimary4,
            textBaseline: TextBaseline.alphabetic
        ),
      ),
      inputDecorationTheme: InputDecorationTheme(
        hintStyle: TextStyle(
          fontSize: 14,
          color: kTheme.inputHint1,
        ),
      ),
      appBarTheme: AppBarTheme(
        elevation: 0.0,
        color: kTheme.colorBackground1,
      ),
      dividerTheme: DividerThemeData(
          color: kTheme.colorLine,
          space: 0.6,
          thickness: 0.6
      ),
      cupertinoOverrideTheme: CupertinoThemeData(
        brightness: Brightness.light,
      ),
      pageTransitionsTheme: NoTransitionsOnWeb(),
      visualDensity: VisualDensity.standard,  // https://github.com/flutter/flutter/issues/77142
    );
  }

  static Size boundingTextSize(String text, TextStyle style,  {int maxLines = 2^31, double maxWidth = double.infinity}) {
    if (text == null || text.isEmpty) {
      return Size.zero;
    }
    final TextPainter textPainter = TextPainter(
        textDirection: TextDirection.ltr,
        text: TextSpan(text: text, style: style), maxLines: maxLines)
      ..layout(maxWidth: maxWidth);
    return textPainter.size;
  }

}
